#!/usr/bin/env python2
# -*- coding: utf-8 -*-
##################################################
# GNU Radio Python Flow Graph
# Title: Stupid Simple Pulsar
# Generated: Mon Apr 11 23:44:26 2022
##################################################


from gnuradio import blocks
from gnuradio import eng_notation
from gnuradio import fft
from gnuradio import filter
from gnuradio import gr
from gnuradio.eng_option import eng_option
from gnuradio.fft import window
from gnuradio.filter import firdes
from optparse import OptionParser
import SimpleXMLRPCServer
import Stupid_folder
import math
import numpy
import st_psr_helper  # embedded python module
import threading
import time


class stupid_simple_pulsar(gr.top_block):

    def __init__(self, antenna="RX2", bw=5e6, dec=54.5, device="uhd,type=b200,subdev=A:A,num_recv_frames=128", dm=26.7641, drate=5000, fine=1, flout=0, fmask="", freq=408e6, logtime=30, longitude=-75.984, obstime=300, period=0.714520, pname="B0329+54", prefix=".", ra=3.5, refclock="external", res=5, rfgain=0, srate=5e6, subdev="A:A", subint=0, width=0.0066):
        gr.top_block.__init__(self, "Stupid Simple Pulsar")

        ##################################################
        # Parameters
        ##################################################
        self.antenna = antenna
        self.bw = bw
        self.dec = dec
        self.device = device
        self.dm = dm
        self.drate = drate
        self.fine = fine
        self.flout = flout
        self.fmask = fmask
        self.freq = freq
        self.logtime = logtime
        self.longitude = longitude
        self.obstime = obstime
        self.period = period
        self.pname = pname
        self.prefix = prefix
        self.ra = ra
        self.refclock = refclock
        self.res = res
        self.rfgain = rfgain
        self.srate = srate
        self.subdev = subdev
        self.subint = subint
        self.width = width

        ##################################################
        # Variables
        ##################################################
        self.sky = sky = freq+1.3e9
        self.skyf = skyf = freq if sky <= 0.0 else sky
        self.samp_rate = samp_rate = int(srate)
        self.fupper = fupper = (skyf+(samp_rate/2.0))/1.0e6
        self.flower = flower = (skyf-(samp_rate/2.0))/1.0e6
        self.smear = smear = dm/2.41e-4 * (1.0/(flower*flower)-1.0/(fupper*fupper))
        self.ideal_fb_bins = ideal_fb_bins = int(max([math.pow(2.0,round(math.log(smear/(width/8.0))/math.log(2.0))), 8]))
        self.fbmap = fbmap = {8 : 10, 16: 20, 32: 40, 64: 80, 128: 125, 256: 250, 512: 500, 1024: 1000, 2048: 2000}
        self.mapped_fbsize = mapped_fbsize = fbmap[ideal_fb_bins]
        self.is7500 = is7500 = int(srate) % int(7500000)
        self.is6250 = is6250 = int(srate) % int(6250000)
        self.mjd = mjd = (time.time() / 86400.0)+40587.0
        self.fbsize = fbsize = int(ideal_fb_bins) if (is6250 > 0  and is7500 > 0) else mapped_fbsize
        self.tbins = tbins = int(period/width)*res
        self.rfi_poller = rfi_poller = [0.0001]*fbsize
        self.rfi_eval_rate = rfi_eval_rate = 2.0
        self.real_fbrate = real_fbrate = st_psr_helper.find_rate(samp_rate,fbsize,drate)
        self.hfilename = hfilename = prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (pname, skyf/1.0e6, samp_rate/1.0e6, bw/1.0e6, obstime/60,fbsize,mjd)
        self.first_poller = first_poller = None
        self.jsfilename = jsfilename = hfilename.replace(".fil",".json")
        self.impulse_poller = impulse_poller = [1.0e7]*fbsize
        self.hdr_sts = hdr_sts = st_psr_helper.build_header_info(hfilename,pname,ra,dec,skyf,samp_rate,real_fbrate,fbsize,flout,first_poller)
        self.flush_poller = flush_poller = 1.0
        self.fine_ppms = fine_ppms = [-1.0,-0.9,-0.8,-0.7,-0.6,-0.5,-0.4,-0.3,-0.2,-0.1,0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]
        self.enable_writes = enable_writes = st_psr_helper.get_wrenabled(first_poller)
        self.current_profile = current_profile = [0.0]*tbins
        self.coarse_ppms = coarse_ppms = [-100,-70,-50,-20,-10,0,10,20,50,70,100]
        self.bank_mask = bank_mask = st_psr_helper.frqlst_to_mask(fmask,skyf,samp_rate,fbsize,rfi_poller,mjd,prefix,pname,rfi_eval_rate)
        self.agc_poller = agc_poller = [1.0]*fbsize

        ##################################################
        # Blocks
        ##################################################
        self.agc_probe = blocks.probe_signal_vf(fbsize)

        def _impulse_poller_probe():
            while True:
                val = self.agc_probe.level()
                try:
                    self.set_impulse_poller(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (1))
        _impulse_poller_thread = threading.Thread(target=_impulse_poller_probe)
        _impulse_poller_thread.daemon = True
        _impulse_poller_thread.start()

        self.rfi_probe = blocks.probe_signal_vf(fbsize)

        def _agc_poller_probe():
            while True:
                val = self.agc_probe.level()
                try:
                    self.set_agc_poller(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (1.0/8.0))
        _agc_poller_thread = threading.Thread(target=_agc_poller_probe)
        _agc_poller_thread.daemon = True
        _agc_poller_thread.start()

        self.Stupid_folder = Stupid_folder.blk(fbsize=fbsize, smear=smear, period=period, fbrate=real_fbrate, tbins=tbins, tppms=fine_ppms if fine > 0 else coarse_ppms, thresh=numpy.sum(impulse_poller))
        self.xmlrpc_server_0 = SimpleXMLRPCServer.SimpleXMLRPCServer(('localhost', 1420), allow_none=True)
        self.xmlrpc_server_0.register_instance(self)
        self.xmlrpc_server_0_thread = threading.Thread(target=self.xmlrpc_server_0.serve_forever)
        self.xmlrpc_server_0_thread.daemon = True
        self.xmlrpc_server_0_thread.start()
        self.single_pole_iir_filter_xx_3 = filter.single_pole_iir_filter_ff(1.0/2.5, fbsize)
        self.single_pole_iir_filter_xx_2 = filter.single_pole_iir_filter_ff(0.03, fbsize)
        self.single_pole_iir_filter_xx_1 = filter.single_pole_iir_filter_ff(1.0/(real_fbrate*3.0), fbsize)
        self.single_pole_iir_filter_xx_0 = filter.single_pole_iir_filter_ff(1.0/(real_fbrate*2.0), fbsize)

        def _rfi_poller_probe():
            while True:
                val = self.rfi_probe.level()
                try:
                    self.set_rfi_poller(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (rfi_eval_rate))
        _rfi_poller_thread = threading.Thread(target=_rfi_poller_probe)
        _rfi_poller_thread.daemon = True
        _rfi_poller_thread.start()


        def _flush_poller_probe():
            while True:
                val = self.Stupid_folder.flush_logfile(skyf,samp_rate,longitude,subint,jsfilename,int(logtime*real_fbrate))
                try:
                    self.set_flush_poller(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (1.0))
        _flush_poller_thread = threading.Thread(target=_flush_poller_probe)
        _flush_poller_thread.daemon = True
        _flush_poller_thread.start()


        def _first_poller_probe():
            while True:
                val = self.Stupid_folder.first_sample_time()
                try:
                    self.set_first_poller(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (50.0))
        _first_poller_thread = threading.Thread(target=_first_poller_probe)
        _first_poller_thread.daemon = True
        _first_poller_thread.start()

        self.fft_vxx_0 = fft.fft_vcc(fbsize, True, (window.blackmanharris(fbsize)), True, 1)

        def _current_profile_probe():
            while True:
                val = self.Stupid_folder.get_profile()
                try:
                    self.set_current_profile(val)
                except AttributeError:
                    pass
                time.sleep(1.0 / (5))
        _current_profile_thread = threading.Thread(target=_current_profile_probe)
        _current_profile_thread.daemon = True
        _current_profile_thread.start()

        self.copyblock = blocks.head(gr.sizeof_gr_complex*1, samp_rate*obstime)
        self.blocks_vector_to_stream_0 = blocks.vector_to_stream(gr.sizeof_float*1, fbsize)
        self.blocks_sub_xx_0 = blocks.sub_ff(fbsize)
        self.blocks_stream_to_vector_0 = blocks.stream_to_vector(gr.sizeof_gr_complex*1, fbsize)
        self.blocks_multiply_const_vxx_2 = blocks.multiply_const_vff((st_psr_helper.process_agc(agc_poller)))
        self.blocks_multiply_const_vxx_1 = blocks.multiply_const_vff(([0.60]*fbsize))
        self.blocks_multiply_const_vxx_0 = blocks.multiply_const_vff((bank_mask))
        self.blocks_integrate_xx_0 = blocks.integrate_ff(int(samp_rate/fbsize/real_fbrate), fbsize)
        self.blocks_float_to_char_0 = blocks.float_to_char(fbsize, 60)
        self.blocks_file_source_0 = blocks.file_source(gr.sizeof_gr_complex*1, '/home/xao/B1/psr-B0329+54-1708.0-5.0-5.0-5.0min-59680.608.bin', False)
        self.blocks_file_sink_1 = blocks.file_sink(gr.sizeof_float*fbsize, "/dev/null" if flout == 0 else hfilename, True)
        self.blocks_file_sink_1.set_unbuffered(True)
        self.blocks_file_sink_0 = blocks.file_sink(gr.sizeof_char*fbsize, hfilename if flout == 0 else "/dev/null", True)
        self.blocks_file_sink_0.set_unbuffered(True)
        self.blocks_copy_0_0 = blocks.copy(gr.sizeof_char*fbsize)
        self.blocks_copy_0_0.set_enabled(enable_writes)
        self.blocks_copy_0 = blocks.copy(gr.sizeof_float*fbsize)
        self.blocks_copy_0.set_enabled(enable_writes)
        self.blocks_conjugate_cc_0 = blocks.conjugate_cc()
        self.blocks_complex_to_mag_squared_0 = blocks.complex_to_mag_squared(fbsize)

        ##################################################
        # Connections
        ##################################################
        self.connect((self.blocks_complex_to_mag_squared_0, 0), (self.blocks_integrate_xx_0, 0))
        self.connect((self.blocks_conjugate_cc_0, 0), (self.blocks_stream_to_vector_0, 0))
        self.connect((self.blocks_copy_0, 0), (self.blocks_file_sink_1, 0))
        self.connect((self.blocks_copy_0_0, 0), (self.blocks_file_sink_0, 0))
        self.connect((self.blocks_file_source_0, 0), (self.copyblock, 0))
        self.connect((self.blocks_float_to_char_0, 0), (self.blocks_copy_0_0, 0))
        self.connect((self.blocks_integrate_xx_0, 0), (self.blocks_multiply_const_vxx_0, 0))
        self.connect((self.blocks_integrate_xx_0, 0), (self.single_pole_iir_filter_xx_2, 0))
        self.connect((self.blocks_multiply_const_vxx_0, 0), (self.single_pole_iir_filter_xx_3, 0))
        self.connect((self.blocks_multiply_const_vxx_1, 0), (self.blocks_sub_xx_0, 1))
        self.connect((self.blocks_multiply_const_vxx_2, 0), (self.blocks_float_to_char_0, 0))
        self.connect((self.blocks_stream_to_vector_0, 0), (self.fft_vxx_0, 0))
        self.connect((self.blocks_sub_xx_0, 0), (self.blocks_copy_0, 0))
        self.connect((self.blocks_sub_xx_0, 0), (self.blocks_multiply_const_vxx_2, 0))
        self.connect((self.blocks_sub_xx_0, 0), (self.blocks_vector_to_stream_0, 0))
        self.connect((self.blocks_sub_xx_0, 0), (self.single_pole_iir_filter_xx_1, 0))
        self.connect((self.blocks_vector_to_stream_0, 0), (self.Stupid_folder, 0))
        self.connect((self.copyblock, 0), (self.blocks_conjugate_cc_0, 0))
        self.connect((self.fft_vxx_0, 0), (self.blocks_complex_to_mag_squared_0, 0))
        self.connect((self.single_pole_iir_filter_xx_0, 0), (self.blocks_multiply_const_vxx_1, 0))
        self.connect((self.single_pole_iir_filter_xx_1, 0), (self.agc_probe, 0))
        self.connect((self.single_pole_iir_filter_xx_2, 0), (self.rfi_probe, 0))
        self.connect((self.single_pole_iir_filter_xx_3, 0), (self.blocks_sub_xx_0, 0))
        self.connect((self.single_pole_iir_filter_xx_3, 0), (self.single_pole_iir_filter_xx_0, 0))

    def get_antenna(self):
        return self.antenna

    def set_antenna(self, antenna):
        self.antenna = antenna

    def get_bw(self):
        return self.bw

    def set_bw(self, bw):
        self.bw = bw
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))

    def get_dec(self):
        return self.dec

    def set_dec(self, dec):
        self.dec = dec
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))

    def get_device(self):
        return self.device

    def set_device(self, device):
        self.device = device

    def get_dm(self):
        return self.dm

    def set_dm(self, dm):
        self.dm = dm
        self.set_smear(self.dm/2.41e-4 * (1.0/(self.flower*self.flower)-1.0/(self.fupper*self.fupper)))

    def get_drate(self):
        return self.drate

    def set_drate(self, drate):
        self.drate = drate
        self.set_real_fbrate(st_psr_helper.find_rate(self.samp_rate,self.fbsize,self.drate))

    def get_fine(self):
        return self.fine

    def set_fine(self, fine):
        self.fine = fine

    def get_flout(self):
        return self.flout

    def set_flout(self, flout):
        self.flout = flout
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))
        self.blocks_file_sink_1.open("/dev/null" if self.flout == 0 else self.hfilename)
        self.blocks_file_sink_0.open(self.hfilename if self.flout == 0 else "/dev/null")

    def get_fmask(self):
        return self.fmask

    def set_fmask(self, fmask):
        self.fmask = fmask
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))

    def get_freq(self):
        return self.freq

    def set_freq(self, freq):
        self.freq = freq
        self.set_skyf(self.freq if self.sky <= 0.0 else self.sky)
        self.set_sky(self.freq+1.3e9)

    def get_logtime(self):
        return self.logtime

    def set_logtime(self, logtime):
        self.logtime = logtime

    def get_longitude(self):
        return self.longitude

    def set_longitude(self, longitude):
        self.longitude = longitude

    def get_obstime(self):
        return self.obstime

    def set_obstime(self, obstime):
        self.obstime = obstime
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.copyblock.set_length(self.samp_rate*self.obstime)

    def get_period(self):
        return self.period

    def set_period(self, period):
        self.period = period
        self.set_tbins(int(self.period/self.width)*self.res)

    def get_pname(self):
        return self.pname

    def set_pname(self, pname):
        self.pname = pname
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))

    def get_prefix(self):
        return self.prefix

    def set_prefix(self, prefix):
        self.prefix = prefix
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))

    def get_ra(self):
        return self.ra

    def set_ra(self, ra):
        self.ra = ra
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))

    def get_refclock(self):
        return self.refclock

    def set_refclock(self, refclock):
        self.refclock = refclock

    def get_res(self):
        return self.res

    def set_res(self, res):
        self.res = res
        self.set_tbins(int(self.period/self.width)*self.res)

    def get_rfgain(self):
        return self.rfgain

    def set_rfgain(self, rfgain):
        self.rfgain = rfgain

    def get_srate(self):
        return self.srate

    def set_srate(self, srate):
        self.srate = srate
        self.set_samp_rate(int(self.srate))
        self.set_is7500(int(self.srate) % int(7500000))
        self.set_is6250(int(self.srate) % int(6250000))

    def get_subdev(self):
        return self.subdev

    def set_subdev(self, subdev):
        self.subdev = subdev

    def get_subint(self):
        return self.subint

    def set_subint(self, subint):
        self.subint = subint

    def get_width(self):
        return self.width

    def set_width(self, width):
        self.width = width
        self.set_tbins(int(self.period/self.width)*self.res)
        self.set_ideal_fb_bins(int(max([math.pow(2.0,round(math.log(self.smear/(self.width/8.0))/math.log(2.0))), 8])))

    def get_sky(self):
        return self.sky

    def set_sky(self, sky):
        self.sky = sky
        self.set_skyf(self.freq if self.sky <= 0.0 else self.sky)

    def get_skyf(self):
        return self.skyf

    def set_skyf(self, skyf):
        self.skyf = skyf
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))
        self.set_fupper((self.skyf+(self.samp_rate/2.0))/1.0e6)
        self.set_flower((self.skyf-(self.samp_rate/2.0))/1.0e6)

    def get_samp_rate(self):
        return self.samp_rate

    def set_samp_rate(self, samp_rate):
        self.samp_rate = samp_rate
        self.set_real_fbrate(st_psr_helper.find_rate(self.samp_rate,self.fbsize,self.drate))
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))
        self.set_fupper((self.skyf+(self.samp_rate/2.0))/1.0e6)
        self.set_flower((self.skyf-(self.samp_rate/2.0))/1.0e6)
        self.copyblock.set_length(self.samp_rate*self.obstime)

    def get_fupper(self):
        return self.fupper

    def set_fupper(self, fupper):
        self.fupper = fupper
        self.set_smear(self.dm/2.41e-4 * (1.0/(self.flower*self.flower)-1.0/(self.fupper*self.fupper)))

    def get_flower(self):
        return self.flower

    def set_flower(self, flower):
        self.flower = flower
        self.set_smear(self.dm/2.41e-4 * (1.0/(self.flower*self.flower)-1.0/(self.fupper*self.fupper)))

    def get_smear(self):
        return self.smear

    def set_smear(self, smear):
        self.smear = smear
        self.set_ideal_fb_bins(int(max([math.pow(2.0,round(math.log(self.smear/(self.width/8.0))/math.log(2.0))), 8])))

    def get_ideal_fb_bins(self):
        return self.ideal_fb_bins

    def set_ideal_fb_bins(self, ideal_fb_bins):
        self.ideal_fb_bins = ideal_fb_bins
        self.set_fbsize(int(self.ideal_fb_bins) if (self.is6250 > 0  and self.is7500 > 0) else self.mapped_fbsize)
        self.set_mapped_fbsize(self.fbmap[self.ideal_fb_bins])

    def get_fbmap(self):
        return self.fbmap

    def set_fbmap(self, fbmap):
        self.fbmap = fbmap
        self.set_mapped_fbsize(self.fbmap[self.ideal_fb_bins])

    def get_mapped_fbsize(self):
        return self.mapped_fbsize

    def set_mapped_fbsize(self, mapped_fbsize):
        self.mapped_fbsize = mapped_fbsize
        self.set_fbsize(int(self.ideal_fb_bins) if (self.is6250 > 0  and self.is7500 > 0) else self.mapped_fbsize)

    def get_is7500(self):
        return self.is7500

    def set_is7500(self, is7500):
        self.is7500 = is7500
        self.set_fbsize(int(self.ideal_fb_bins) if (self.is6250 > 0  and self.is7500 > 0) else self.mapped_fbsize)

    def get_is6250(self):
        return self.is6250

    def set_is6250(self, is6250):
        self.is6250 = is6250
        self.set_fbsize(int(self.ideal_fb_bins) if (self.is6250 > 0  and self.is7500 > 0) else self.mapped_fbsize)

    def get_mjd(self):
        return self.mjd

    def set_mjd(self, mjd):
        self.mjd = mjd
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))

    def get_fbsize(self):
        return self.fbsize

    def set_fbsize(self, fbsize):
        self.fbsize = fbsize
        self.set_real_fbrate(st_psr_helper.find_rate(self.samp_rate,self.fbsize,self.drate))
        self.set_impulse_poller([1.0e7]*self.fbsize)
        self.set_hfilename(self.prefix+"/D1/psr-%s-%-.1f-%-.1f-%-.1f-%-.1f-%d-%9.3f.fil" % (self.pname, self.skyf/1.0e6, self.samp_rate/1.0e6, self.bw/1.0e6, self.obstime/60,self.fbsize,self.mjd))
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))
        self.set_agc_poller([1.0]*self.fbsize)
        self.set_rfi_poller([0.0001]*self.fbsize)
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))
        self.blocks_multiply_const_vxx_1.set_k(([0.60]*self.fbsize))

    def get_tbins(self):
        return self.tbins

    def set_tbins(self, tbins):
        self.tbins = tbins
        self.set_current_profile([0.0]*self.tbins)

    def get_rfi_poller(self):
        return self.rfi_poller

    def set_rfi_poller(self, rfi_poller):
        self.rfi_poller = rfi_poller
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))

    def get_rfi_eval_rate(self):
        return self.rfi_eval_rate

    def set_rfi_eval_rate(self, rfi_eval_rate):
        self.rfi_eval_rate = rfi_eval_rate
        self.set_bank_mask(st_psr_helper.frqlst_to_mask(self.fmask,self.skyf,self.samp_rate,self.fbsize,self.rfi_poller,self.mjd,self.prefix,self.pname,self.rfi_eval_rate))

    def get_real_fbrate(self):
        return self.real_fbrate

    def set_real_fbrate(self, real_fbrate):
        self.real_fbrate = real_fbrate
        self.single_pole_iir_filter_xx_1.set_taps(1.0/(self.real_fbrate*3.0))
        self.single_pole_iir_filter_xx_0.set_taps(1.0/(self.real_fbrate*2.0))
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))

    def get_hfilename(self):
        return self.hfilename

    def set_hfilename(self, hfilename):
        self.hfilename = hfilename
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))
        self.blocks_file_sink_1.open("/dev/null" if self.flout == 0 else self.hfilename)
        self.blocks_file_sink_0.open(self.hfilename if self.flout == 0 else "/dev/null")

    def get_first_poller(self):
        return self.first_poller

    def set_first_poller(self, first_poller):
        self.first_poller = first_poller
        self.set_enable_writes(st_psr_helper.get_wrenabled(self.first_poller))
        self.set_hdr_sts(st_psr_helper.build_header_info(self.hfilename,self.pname,self.ra,self.dec,self.skyf,self.samp_rate,self.real_fbrate,self.fbsize,self.flout,self.first_poller))

    def get_jsfilename(self):
        return self.jsfilename

    def set_jsfilename(self, jsfilename):
        self.jsfilename = jsfilename

    def get_impulse_poller(self):
        return self.impulse_poller

    def set_impulse_poller(self, impulse_poller):
        self.impulse_poller = impulse_poller
        self.Stupid_folder.thresh = numpy.sum(self.impulse_poller)

    def get_hdr_sts(self):
        return self.hdr_sts

    def set_hdr_sts(self, hdr_sts):
        self.hdr_sts = hdr_sts

    def get_flush_poller(self):
        return self.flush_poller

    def set_flush_poller(self, flush_poller):
        self.flush_poller = flush_poller

    def get_fine_ppms(self):
        return self.fine_ppms

    def set_fine_ppms(self, fine_ppms):
        self.fine_ppms = fine_ppms

    def get_enable_writes(self):
        return self.enable_writes

    def set_enable_writes(self, enable_writes):
        self.enable_writes = enable_writes
        self.blocks_copy_0_0.set_enabled(self.enable_writes)
        self.blocks_copy_0.set_enabled(self.enable_writes)

    def get_current_profile(self):
        return self.current_profile

    def set_current_profile(self, current_profile):
        self.current_profile = current_profile

    def get_coarse_ppms(self):
        return self.coarse_ppms

    def set_coarse_ppms(self, coarse_ppms):
        self.coarse_ppms = coarse_ppms

    def get_bank_mask(self):
        return self.bank_mask

    def set_bank_mask(self, bank_mask):
        self.bank_mask = bank_mask
        self.blocks_multiply_const_vxx_0.set_k((self.bank_mask))

    def get_agc_poller(self):
        return self.agc_poller

    def set_agc_poller(self, agc_poller):
        self.agc_poller = agc_poller
        self.blocks_multiply_const_vxx_2.set_k((st_psr_helper.process_agc(self.agc_poller)))


def argument_parser():
    parser = OptionParser(usage="%prog: [options]", option_class=eng_option)
    parser.add_option(
        "", "--antenna", dest="antenna", type="string", default="RX2",
        help="Set Antenna selector [default=%default]")
    parser.add_option(
        "", "--bw", dest="bw", type="eng_float", default=eng_notation.num_to_str(5e6),
        help="Set Band Width [default=%default]")
    parser.add_option(
        "", "--dec", dest="dec", type="eng_float", default=eng_notation.num_to_str(54.5),
        help="Set Source DEC [default=%default]")
    parser.add_option(
        "", "--device", dest="device", type="string", default="uhd,type=b200,subdev=A:A,num_recv_frames=128",
        help="Set Device String [default=%default]")
    parser.add_option(
        "", "--dm", dest="dm", type="eng_float", default=eng_notation.num_to_str(26.7641),
        help="Set Dispersion Measure [default=%default]")
    parser.add_option(
        "", "--drate", dest="drate", type="intx", default=5000,
        help="Set Filterbank output rate(sample rate 64us15625,400us/2500) [default=%default]")
    parser.add_option(
        "", "--fine", dest="fine", type="intx", default=1,
        help="Set Set fine-scale trial folding [default=%default]")
    parser.add_option(
        "", "--flout", dest="flout", type="intx", default=0,
        help="Set Enable floating-point FB output [default=%default]")
    parser.add_option(
        "", "--fmask", dest="fmask", type="string", default="",
        help="Set Frequency Mask [default=%default]")
    parser.add_option(
        "", "--freq", dest="freq", type="eng_float", default=eng_notation.num_to_str(408e6),
        help="Set Frequency (in Hz) [default=%default]")
    parser.add_option(
        "", "--logtime", dest="logtime", type="intx", default=30,
        help="Set Logging interval (seconds) [default=%default]")
    parser.add_option(
        "", "--longitude", dest="longitude", type="eng_float", default=eng_notation.num_to_str(-75.984),
        help="Set WGS84 Longitude [default=%default]")
    parser.add_option(
        "", "--obstime", dest="obstime", type="intx", default=300,
        help="Set Runtime in seconds [default=%default]")
    parser.add_option(
        "", "--period", dest="period", type="eng_float", default=eng_notation.num_to_str(0.714520),
        help="Set Pulsar period (seconds) [default=%default]")
    parser.add_option(
        "", "--pname", dest="pname", type="string", default="B0329+54",
        help="Set Pulsar Name [default=%default]")
    parser.add_option(
        "", "--prefix", dest="prefix", type="string", default=".",
        help="Set filename prefix [default=%default]")
    parser.add_option(
        "", "--ra", dest="ra", type="eng_float", default=eng_notation.num_to_str(3.5),
        help="Set Source RA [default=%default]")
    parser.add_option(
        "", "--refclock", dest="refclock", type="string", default="external",
        help="Set Reference clock source [default=%default]")
    parser.add_option(
        "", "--res", dest="res", type="intx", default=5,
        help="Set Time bin resolution multiplier [default=%default]")
    parser.add_option(
        "", "--rfgain", dest="rfgain", type="eng_float", default=eng_notation.num_to_str(0),
        help="Set RF Gain [default=%default]")
    parser.add_option(
        "", "--srate", dest="srate", type="eng_float", default=eng_notation.num_to_str(5e6),
        help="Set Sample Rate (in Hz) [default=%default]")
    parser.add_option(
        "", "--subdev", dest="subdev", type="string", default="A:A",
        help="Set Subdev (UHD only) [default=%default]")
    parser.add_option(
        "", "--subint", dest="subint", type="intx", default=0,
        help="Set Sub integration interval [default=%default]")
    parser.add_option(
        "", "--width", dest="width", type="eng_float", default=eng_notation.num_to_str(0.0066),
        help="Set Pulsar W50 width (seconds) [default=%default]")
    return parser


def main(top_block_cls=stupid_simple_pulsar, options=None):
    if options is None:
        options, _ = argument_parser().parse_args()

    tb = top_block_cls(antenna=options.antenna, bw=options.bw, dec=options.dec, device=options.device, dm=options.dm, drate=options.drate, fine=options.fine, flout=options.flout, fmask=options.fmask, freq=options.freq, logtime=options.logtime, longitude=options.longitude, obstime=options.obstime, period=options.period, pname=options.pname, prefix=options.prefix, ra=options.ra, refclock=options.refclock, res=options.res, rfgain=options.rfgain, srate=options.srate, subdev=options.subdev, subint=options.subint, width=options.width)
    tb.start()
    tb.wait()


if __name__ == '__main__':
    main()
